﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Android_Helper.Utils
{
    class NameUtils
    {
        static List<string> sysColumns = new List<string>();
        static List<string> insertNoColumns = new List<string>();

        /// <summary>
        /// 普通汉字转英文并转为小写下划线格式
        /// </summary>
        /// <param name="word">汉字</param>
        /// <returns>小写 下划线格式。如数据库表名、字段名</returns>
        public static string HanZiToLowercaseUnderline(String word)
        {
            string result = word;
            result = result.Replace(" ", "");
            result = result.Replace("　", "");
            result = xUtils.YouDaoTranslateTool(result);
            //foreach (string str in word.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
            //{
            //    result += str.First().ToString().ToUpper() + str.Substring(1) + "_";
            //}
            //result = result.Substring(0, result.Length - 1);
            result = result.Replace(" ", "_");
            return result;
        }

        /// <summary>
        /// 首字母全大写
        /// </summary>
        /// <param name="value">字段名</param>
        /// <returns>首字母全大写的</returns>
        public static string LowercaseUnderlineToAllCapitalUpper(string value) {
            if (string.IsNullOrEmpty(value)) {
                return value;
            }
            if (!value.Contains("_")) { 
                return value.Substring(0, 1).ToUpper() + value.Substring(1);
            }
            string[] strs = value.Split('_');
            string ret = "";
            foreach (string str in strs) { 
                ret+= str.Substring(0, 1).ToUpper() + str.Substring(1);
            }
            return ret;
        }

        /// <summary>
        /// 字段名转驼峰写法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string LowercaseUnderlineToCammle(string value)
        {
            string ret = LowercaseUnderlineToAllCapitalUpper(value);
            if (!string.IsNullOrEmpty(ret) && (ret.Length > 1)) { 
                ret = ret.Substring(0, 1).ToLower() + ret.Substring(1);
            }
            return ret;
        }

        /// <summary>
        /// GoLang语言特殊写法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GoLangDialectID(string value) {
            if (string.IsNullOrEmpty(value)) {
                return value;
            }
            string ret = value;
            ret = ret.Replace("Url", "URL");
            ret = ret.Replace("Id", "ID");
            return ret;
        }

        /// <summary>
        /// 是否为系统字段
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static bool IsSystemColume(string columnName) {
            if (sysColumns.Count == 0) {
                sysColumns.Add("id");
                sysColumns.Add("create_time");
                sysColumns.Add("last_update_time");
                sysColumns.Add("sys_remark");
                sysColumns.Add("operator_type");
                sysColumns.Add("operator_id");
                sysColumns.Add("be_active");
                sysColumns.Add("sys_row_editable");
                sysColumns.Add("sys_row_delete");
                sysColumns.Add("version");
            }
            return sysColumns.Contains(columnName);
        }

        

        /// <summary>
        /// insert禁用字段
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static bool IsInsertNoColumns(string columnName)
        {
            if (insertNoColumns.Count == 0)
            {
                insertNoColumns.Add("id");
            }
            return insertNoColumns.Contains(columnName);
        }
    }
}
